package org.csu.question.entity.dto;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class QuestionDTO {
    private Long id;

    @NotBlank(message = "题目标题不能为空")
    private String title;

    @NotBlank(message = "题目内容不能为空")
    private String content;

    // 在返回给前端时不包含答案
    @JsonIgnore
    private String answer;

    private String options; // JSON格式的选项

    private String difficulty;

    private String explanation;

    private Long knowledgePointId;

    private String knowledgePointName;

    private String tag;

    private LocalDateTime createdAt;

    private LocalDateTime updatedAt;

    // 解析选项JSON为列表
    public List<String> getOptionsList() {
        if (StringUtils.hasText(options)) {
            try {
                return JSON.parseArray(options, String.class);
            } catch (Exception e) {
                return Collections.emptyList();
            }
        }
        return Collections.emptyList();
    }

    // 设置选项列表为JSON
    public void setOptionsList(List<String> optionsList) {
        if (optionsList != null && !optionsList.isEmpty()) {
            this.options = JSON.toJSONString(optionsList);
        }
    }
}
